package cmcc.gz.cgt.generator;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import cmcc.gz.cgt.bean.DatabaseBean;
import cmcc.gz.cgt.bean.TableBean;

/**
 * 代码生成类
 * 
 * @ClassName: DomainGenerator
 * @author AndrewWen
 * @date 2013-1-14 下午8:45:30
 */
public class CodeGenerator {
	
	//是否去掉模块前面的编号
	public static String flag="0";

	public CodeGenerator() {
		
	}

	
	public void config(String classPackage, String author, String codePath,
			String classDriver, String url, String username, String password) {
		
		this.classPackage = classPackage;
		this.author = author;
		this.codePath = codePath;
		this.classDriver = classDriver;
		this.url = url;
		this.username = username;
		this.password = password;
		
		sourcePath=codePath+"src/main/java/";
		
		testPath=codePath+"src/test/java/";
		
		jspPath=codePath+"src/main/webapp/";
		
	}

	/**
	 * 包名
	 */
	private String classPackage="cmcc.gz.micc";
	
	/**
	 * 作者
	 */
	private String author="Andrew.Wen";
	
	/**
	 * 代码生成路径
	 */
	private String codePath="D:/codeGenerator/source/";
	
	private String classDriver="com.mysql.jdbc.Driver";
    private String url="jdbc:mysql://localhost:3306/mic";
    private String username="root";
    private String password="root123";
	
	/**
	 * 生成代码路径
	 */
	private String sourcePath=codePath+"src/main/java/";
	
	private String testPath=codePath+"src/test/java/";
	
	private String jspPath=codePath+"src/main/webapp/";
	
	

	public static void main(String[] args) throws SQLException {

		//CodeGenerator dg=new CodeGenerator(args[0],args[1],args[2],args[3],args[4],args[5],args[6]);
		CodeGenerator dg=new CodeGenerator();
		
		
		String propertFilePath="config.properties";
		
		String classPackage = StringUtil.getPropertyFromFile(propertFilePath, "classPackage");
		String author =StringUtil.getPropertyFromFile(propertFilePath, "author"); 
		String codePath =StringUtil.getPropertyFromFile(propertFilePath, "codePath");  
		String classDriver =StringUtil.getPropertyFromFile(propertFilePath, "classDriver");  
		String url =StringUtil.getPropertyFromFile(propertFilePath, "url");  
		String username =StringUtil.getPropertyFromFile(propertFilePath, "username");  
		String password =StringUtil.getPropertyFromFile(propertFilePath, "password");  
		
		String tableNamePattern =StringUtil.getPropertyFromFile(propertFilePath, "tableNamePattern");  
		
		
		dg.config(classPackage,author,codePath,classDriver,url,username,password);

		
		//dg.config(args[0],args[1],args[2],args[3],args[4],args[5],args[6]);
		
		dg.generator(tableNamePattern);
		
	}

	public boolean generator(String tableNamePattern) {
		
		/**
		 * 数据库
		 */
		DatabaseBean databaseBean=null;
		
		/**
		 * 数据库表
		 */
		List<TableBean> tableList = null;
		
		try {
			databaseBean = new DatabaseInfoOp(classDriver,url,username,password).getDbInfo(tableNamePattern);
			tableList = databaseBean.getTableList();
		
		} catch (Exception e1) {
			e1.printStackTrace();
		}

	

		/**
		 * 遍历生成代码
		 */
		for (TableBean temptb : tableList) {
			
			temptb.setDomainPackage(this.classPackage);

			Map<String, Object> map = new HashMap<String, Object>();

			map.put("table", temptb);
			map.put("author", this.author);
			
		
			System.out.println("生成表："+temptb.getTableName());
			//生成实体
			VelocityInfoOp.generatorCode("DomainTemplate.vm", map,this.sourcePath + temptb.getDomainPath()+"/db/domain",temptb.getDomainClassName() + ".java");
			
			//生成对应文件xml
			VelocityInfoOp.generatorCode("DomainMapperXmlTemplate.vm",map,this.sourcePath+ temptb.getDomainPath()+ "/db/mapper", temptb.getDomainClassName() + "Mapper.xml");
			
			//生成对应文件
			VelocityInfoOp.generatorCode("DomainMapperTemplate.vm",map,this.sourcePath+ temptb.getDomainPath()+ "/db/mapper", temptb.getDomainClassName() + "Mapper.java");
			
			//生成业务逻辑类
			VelocityInfoOp.generatorCode("ServiceTemplate.vm",map,this.sourcePath+ temptb.getDomainPath()+ "/service", temptb.getDomainClassName() + "Service.java");
			
			//生成控制器类
			VelocityInfoOp.generatorCode("ControllerTemplate.vm",map,this.sourcePath+ temptb.getDomainPath()+ "/controller", temptb.getDomainClassName() + "Controller.java");
			
			//单元测试类
			VelocityInfoOp.generatorCode("UnitTestTemplate.vm",map,this.testPath+ temptb.getDomainPath()+ "/service/test", temptb.getDomainClassName() + "ServiceTest.java");
			
			//jsp列表显示页面
			VelocityInfoOp.generatorCode("JqueryjspListTemplate.vm",map,this.jspPath+ "jsp", temptb.getLowerDomainClassName() + ".jsp");
			
			//jsp增加页面
			//VelocityInfoOp.generatorCode("jspAddTemplate.vm",map,this.jspPath+ "jsp", temptb.getLowerDomainClassName() + "_add.jsp");
			//jsp修改页面
			VelocityInfoOp.generatorCode("jspEditTemplate.vm",map,this.jspPath+ "jsp", temptb.getLowerDomainClassName() + "_edit.jsp");
			
			//Extjs列表页面
			VelocityInfoOp.generatorCode("JqueryTemplate.vm",map,this.jspPath+ "jsp"+ "/js", temptb.getLowerDomainClassName() + ".js");
			
		}
		
		
		System.out.println("***************代码生成完成******************");
		System.out.println("代码路径："+this.codePath);
		System.out.println("包："+this.classPackage);
		System.out.println("作者："+this.author);
		System.out.println("********************************************");
		
		return true;
	}
	
	

	
	public String getClassPackage() {
		return classPackage;
	}

	public void setClassPackage(String classPackage) {
		this.classPackage = classPackage;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public String getPath() {
		return sourcePath;
	}

	public void setPath(String path) {
		this.sourcePath = path;
	}

	public String getJspPath() {
		return jspPath;
	}


	public void setJspPath(String jspPath) {
		this.jspPath = jspPath;
	}



}
